package com.liang.jiekou.service.impl;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.entity.yifei.Purth;
import com.liang.jiekou.entity.*;
import com.liang.jiekou.entity.dto.*;
import com.liang.jiekou.entity.result.COPTCD;
import com.liang.jiekou.entity.result.PURTCD;
import com.liang.jiekou.entity.vo.RaxVo;
import com.liang.jiekou.mapper.PURMAMapper;
import com.liang.jiekou.mapper.PURTCDMapper;
import com.liang.jiekou.mapper.PURTGMapper;
import com.liang.jiekou.mapper.PURTHMapper;
import com.liang.jiekou.service.HttpClientService;
import com.liang.jiekou.service.PURTCDService;
import com.liang.jiekou.untils.HttpClientUtil;
import com.liang.jiekou.untils.MoneyUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.xml.bind.annotation.W3CDomHandler;
import java.lang.ref.WeakReference;
import java.math.BigDecimal;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class PURTCDServiceImpl implements PURTCDService {

    @Resource
    private PURTCDMapper purtcdMapper;

    @Resource
    private PURTGMapper purtgMapper;

    @Resource
    private PURTHMapper purthMapper;

    @Resource
    private PURMAMapper purmaMapper;

    @Resource
    private HttpClientService httpClientService;

    @Override
    public PURTCD selectPURTCD(String TC001, String TC002) {
        PURTC purtc = purtcdMapper.selectPURTC(TC001, TC002);

        List<PURTD> purtdList = purtcdMapper.selectPURTD(TC001, TC002);

        //将查询的结果赋值给采购单总类实体
        PURTCD purtcd = new PURTCD();
        purtcd.setPurtc(purtc);
        purtcd.setPurtdList(purtdList);

        return purtcd;
    }

    @Override
    public IPage<PURTCD> selectCOPTCDall(Page<PURTCD> page, String startcreate, String endcreate, String startupdate, String endupdate) {
        IPage<PURTC> purtciPage = purtcdMapper.selectPURTCall(page, startcreate, endcreate, startupdate, endupdate);
        List<PURTC> purtcList = purtciPage.getRecords();

        IPage<PURTD> purtdiPage = purtcdMapper.selectPURTDall(page, startcreate, endcreate, startupdate, endupdate);
        List<PURTD> purtdList = purtdiPage.getRecords();

        List<PURTCD> purtcdList = new ArrayList<>();
        for (PURTC purtc : purtcList) {
            PURTCD purtcd = new PURTCD();
            purtcd.setPurtc(purtc);
            List<PURTD> matchedPurtdList = new ArrayList<>();
            for (PURTD purtd : purtdList) {
                if (purtc.getTC001().equals(purtd.getTD001()) && purtc.getTC002().equals(purtd.getTD002())) {
                    matchedPurtdList.add(purtd);
                }
            }
            purtcd.setPurtdList(matchedPurtdList);
            purtcdList.add(purtcd);

        }

        IPage<PURTCD> resultPage = new Page<>(page.getCurrent(), page.getSize(), purtciPage.getTotal());
        resultPage.setRecords(purtcdList);

        return resultPage;
    }

    @Override
    public RpcServiceResult<Map<String, Object>> insertjhd(JhdDto jhdDto) throws UnknownHostException {
        int i = 1;
        Double jhje = 0.0;
        Double kkje = 0.0;
        Double zjs = 0.0;
        Double zyssl = 0.0;
        Double bbdkje = 0.0;
        Double bbse = 0.0;
        Double zysbzsl = 0.0;
        Double jhsl = 0.0;
        Double jhbzsl = 0.0;

        Purtg purtg = new Purtg();

        Map<String, Object> list = new HashMap<>();
        purtg.setTg001("3410");
        Long dh = purtgMapper.getDH();

        // 获取当前时间
        LocalDateTime currentTime = LocalDateTime.now();

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMM");
        DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyyMMdd");
        DateTimeFormatter create_date = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");

        // 格式化时间
        String formattedTime = currentTime.format(formatter);
        String yyyyMMdd = currentTime.format(formatter1);

        Long time = Long.parseLong(formattedTime + "0001");
        if (dh < time) {
            dh = time;
        } else {
            dh++;
        }

        purtg.setTg002("" + dh);

        purtg.setCompany("ttc");
        purtg.setCreator(jhdDto.getCreator());
        purtg.setUsrGroup(purtgMapper.getUsrGroup(jhdDto.getCreator()));
        purtg.setCreateDate(currentTime.format(create_date));
        purtg.setFlag("2");

        purtg.setTg003(yyyyMMdd);

        //工厂只有一个，先写死了
        purtg.setTg004("1");

        purtg.setTg005(jhdDto.getWorkCenter());
        purtg.setTg006(jhdDto.getWorkCenterNo());

        LambdaQueryWrapper<PURMA> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PURMA::getMA001, jhdDto.getWorkCenter());
        PURMA purma = purmaMapper.selectOne(wrapper);
        purtg.setTg007(purma.getMA021());

        if (purma.getMA021().trim().equals("RMB")) {
            purtg.setTg008(1.0000000);
        } else {
            Double hl = purtgMapper.selectHL(purma.getMA021());
            purtg.setTg008(hl);
        }
        purtg.setTg009(purma.getMA030());
        purtg.setTg010(purma.getMA044());
        purtg.setTg012(0.0);
        purtg.setTg013("N");

        purtg.setTg014(yyyyMMdd);
        purtg.setTg015("N");
        purtg.setTg016(jhdDto.getTG016());
        List<Purth> purths = new ArrayList<>();
        List<PURTHDto> purthDtoList = jhdDto.getPurthDtoList();
        for (PURTHDto purthDto : purthDtoList) {
            Purth purth = new Purth();
            String xuh = String.format("%04d", i++);
            Integer pd = purthMapper.isExistToTheInvoice(purthDto.getDhjydb(), purthDto.getDhjydh(), purthDto.getXh(), purtg.getTg005());
            if (pd < 0) {
                return RpcServiceResult.getFailureResult("1000", "not fount Inspection batch Please re-select");
            } else {
                list = purthMapper.Select(purthDto.getDhjydb(), purthDto.getDhjydh(), purthDto.getXh(), purtg.getTg005());
            }
            purth.setCompany("ttc");
            purth.setCreator(jhdDto.getCreator());
            purth.setUsrGroup(purtgMapper.getUsrGroup(jhdDto.getCreator()));
            purth.setCreateDate(currentTime.format(create_date));
            purth.setFlag("2");
            purth.setTh001("3410");
            purth.setTh002("" + dh);
            purth.setTh003(xuh);
            purth.setTh004((String) list.get("CD004"));
            purth.setTh005((String) list.get("CD005"));
            purth.setTh006((String) list.get("CD006"));
            BigDecimal bigDecimal = (BigDecimal) list.get("CD013");
            purth.setTh007(bigDecimal.doubleValue());
            jhsl += bigDecimal.doubleValue();
            purth.setTh008((String) list.get("CD007"));
            purth.setTh009((String) list.get("CD016"));
            purth.setTh010("********************");
            purth.setTh011((String) list.get("CD010"));
            purth.setTh012((String) list.get("CD011"));
            purth.setTh013((String) list.get("CD012"));
            purth.setTh014(yyyyMMdd);
            BigDecimal TAC01 = (BigDecimal) list.get("TAC01");
            purth.setTh015(TAC01.doubleValue());
            zyssl += TAC01.doubleValue();
            Boolean isSame = purthMapper.isSameUnit((String) list.get("CD004"));
            if (isSame) {
                purth.setTh016(TAC01.doubleValue());
            } else {
                Double calculate = purthMapper.calculate(TAC01.doubleValue(), (String) list.get("CD004"));
                purth.setTh016(calculate);
            }
            BigDecimal TA008 = (BigDecimal) list.get("TA008");
            purth.setTh017(TA008.doubleValue());
            Double DJ = purtcdMapper.selectOne((String) list.get("CD011"), (String) list.get("CD012"));
            purth.setTh018(DJ);
            purth.setTh019(DJ * TAC01.doubleValue());
            jhje += DJ * TAC01.doubleValue();
            purth.setTh020(DJ * TA008.doubleValue());
            kkje = DJ * TA008.doubleValue();
            purth.setTh024(0.00);
            purth.setTh026("N");
            Boolean CQ = purtcdMapper.selectCQ((String) list.get("CD011"), (String) list.get("CD012"));
            if (CQ) {
                purth.setTh027("Y");
            } else {
                purth.setTh027("N");
            }
            purth.setTh028("2");
            purth.setTh029("N");
            purth.setTh030("N");
            purth.setTh031("N");
            purth.setTh032("N");
            purth.setTh033(purthDto.getBz());
            purth.setTh034(TAC01.doubleValue());
            purth.setTh038(jhdDto.getCreator());
            purth.setTh043("N");
            purth.setTh044("N");
            //TODO 这边取得不够完善
            purth.setTh045(DJ * TAC01.doubleValue());
            BigDecimal MA064 = (BigDecimal) list.get("MA064");
            purth.setTh046(DJ * MA064.doubleValue());
            purth.setTh047(DJ * TAC01.doubleValue());
            bbdkje += DJ * TAC01.doubleValue();
            purth.setTh048(DJ * MA064.doubleValue());
            bbse += DJ * MA064.doubleValue();
            BigDecimal CD023 = (BigDecimal) list.get("CD023");
            purth.setTh049(CD023.doubleValue());
            jhbzsl += CD023.doubleValue();
            BigDecimal TAC03 = (BigDecimal) list.get("TAC03");
            purth.setTh050(TAC03.doubleValue());
            zysbzsl += TAC03.doubleValue();
            BigDecimal TA041 = (BigDecimal) list.get("TA041");
            purth.setTh051(TA041.doubleValue());
            purth.setTh052(0.00);
            purth.setTh054(0.000000);
            purth.setTh055(0.00);
            BigDecimal CD031 = (BigDecimal) list.get("CD031");
            purth.setTh058(CD031.doubleValue());
            BigDecimal CD032 = (BigDecimal) list.get("CD032");
            purth.setTh059(CD032.doubleValue());
            zjs += CD032.doubleValue();
            purth.setTh060(0.0000);
            BigDecimal TA025 = (BigDecimal) list.get("TA025");
            purth.setTh061(TA025.doubleValue());
            BigDecimal TA042 = (BigDecimal) list.get("TA042");
            purth.setTh062(TA042.doubleValue());
            String jjdw = purthMapper.getJJDW((String) list.get("CD004"));
            purth.setTh064(jjdw);
            purth.setTh065((String) list.get("CD021"));
            BigDecimal TA007 = (BigDecimal) list.get("TA007");
            purth.setTh068(TA007.doubleValue());
            BigDecimal TA031 = (BigDecimal) list.get("TA031");
            purth.setTh069(TA031.doubleValue());
            purth.setTh070("N");
            BigDecimal TA028 = (BigDecimal) list.get("TA028");
            purth.setTh071(TA028.doubleValue());
            purth.setTh072("##########");
            purth.setTh077(0.000000);
            purth.setTh078(0.000000);
            purth.setTh079(0.000000);
            purth.setTh080("N");
            purth.setThc01(0.0);
            purth.setThc02("2");
            purth.setThc03((String) list.get("TA001"));
            purth.setThc04((String) list.get("TA002"));
            purth.setThc05((String) list.get("TA003"));
            purth.setThc06((String) list.get("TAC04"));
            purth.setThc07("0001");
            purths.add(purth);
        }
        //TODO 进货金额需要去采购单里面取
        purtg.setTg017(jhje);
        purtg.setTg018(kkje);
        purtg.setTg019(jhje * purma.getMA064().doubleValue());
        purtg.setTg020(0.00);
        purtg.setTg021(purma.getMA003());
        purtg.setTg022(purma.getMA005());
        purtg.setTg023("1");
        purtg.setTg023("N");
        purtg.setTg025(zjs);
        purtg.setTg026(zyssl);
        purtg.setTg028(jhje);
        purtg.setTg030(purma.getMA064().doubleValue());
        purtg.setTg031(bbdkje);
        purtg.setTg032(bbse);
        purtg.setTg038(0.000000);
        purtg.setTg039(0.000000);
        purtg.setTg040(zysbzsl);
        purtg.setTg041(0.000000);
        purtg.setTg042("N");
        purtg.setTg045(0.0);
        purtg.setTg046(0.0);
        purtg.setTg050("N");
        purtg.setTg053(jhsl);
        purtg.setTg054(jhbzsl);
        purtg.setTg055(0.000000);
        purtg.setTg058("0");

        purtgMapper.insert(purtg);

        for (Purth purth1 : purths) {
            purthMapper.insert(purth1);
        }

        ShenHe shenHe = new ShenHe();
        shenHe.setCom("ttc");
        shenHe.setProgram("PURI09");
        shenHe.setUser(jhdDto.getCreator());
        shenHe.setDb("3410");
        shenHe.setDh(""+dh);
        shenHe.setType("DO");
        shenHe.setRq(yyyyMMdd);

        InetAddress localhost = InetAddress.getLocalHost();
        String ipAddress = localhost.getHostAddress();
        String url = "http://" + ipAddress + ":9000/api/yifei/audit";


        String jsonString = JSON.toJSONString(shenHe);
        String ans = null;
        try {
//            ShenHe anss=restTemplate.postForObject(url,shenHe,ShenHe.class);
            ans = httpClientService.postRequest(url, jsonString);
        }catch (Exception e){
            //System.out.println("出现异常请查看审批后台是否开启");
            log.info("出现异常请查看审批后台是否开启");
            System.out.println(e);
            return RpcServiceResult.getFailureResult("-10000", "出现异常请查看审批后台是否开启");
        }

        HttpResult httpResult = new HttpResult();

        if (ans == null)
        {
            System.out.println("出现异常请查看审批后台是否开启");
            return RpcServiceResult.getFailureResult("-10000", "出现异常请查看审批后台是否开启");
        }
        else {
            ans = ans.replace("\\r","");

            System.out.println("ans :"  + ans);
            httpResult = JSON.parseObject(ans,HttpResult.class);
//            System.out.println("httpResult: " + httpResult);

        }
        if (httpResult.getStatus().equals("失败"))
        {
            purtgMapper.deletePurtg(purtg);
            for (Purth purth1 : purths) {
                purthMapper.deletePurth(purth1);
            }
            return RpcServiceResult.getFailureResult("-10001", "审核失败！\\n" + "原因：" + httpResult.getReason() + "\\n 详情：" + httpResult.getDetail());
        }


        Map<String, Object> map = new HashMap<>();
        map.put("result", "success");
        map.put("db-dh","3420-"+dh);
        return RpcServiceResult.getSuccessResult(map);
    }

    @Override
    public RpcServiceResult<Map<String, Object>> insertjhdByPur(JhdByPurDto jhdDto) throws UnknownHostException {
        int i = 1;
        Double jhje = 0.0;
        Double kkje = 0.0;
        Double zjs = 0.0;
        Double zyssl = 0.0;
        Double bbdkje = 0.0;
        Double bbse = 0.0;
        Double zysbzsl = 0.0;
        Double jhsl = 0.0;
        Double jhbzsl = 0.0;
        Double ybse = 0.0;

        Purtg purtg = new Purtg();

        Map<String, Object> list = new HashMap<>();
        purtg.setTg001("3420");
        Long dh = purtgMapper.getDH();

        // 获取当前时间
        LocalDateTime currentTime = LocalDateTime.now();

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMM");
        DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyyMMdd");
        DateTimeFormatter create_date = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");

        // 格式化时间
        String formattedTime = currentTime.format(formatter);
        String yyyyMMdd = currentTime.format(formatter1);

        Long time = Long.parseLong(formattedTime + "0001");
        if (dh < time) {
            dh = time;
        } else {
            dh++;
        }

        purtg.setTg002("" + dh);

        purtg.setCompany("ttc");
        purtg.setCreator(jhdDto.getCreator());
        purtg.setUsrGroup(purtgMapper.getUsrGroup(jhdDto.getCreator()));
        purtg.setCreateDate(currentTime.format(create_date));
        purtg.setFlag("2");

        purtg.setTg003(yyyyMMdd);

        //工厂只有一个，先写死了
        purtg.setTg004("1");

        purtg.setTg005(jhdDto.getWorkCenter());
        purtg.setTg006(jhdDto.getWorkCenterNo());

        LambdaQueryWrapper<PURMA> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PURMA::getMA001, jhdDto.getWorkCenter());
        PURMA purma = purmaMapper.selectOne(wrapper);
        purtg.setTg007(purma.getMA021());
        Double hl = 1.00;
        if (purma.getMA021().trim().equals("RMB")) {
            hl = 1.0000000;
            purtg.setTg008(1.0000000);
        } else {
            hl = purtgMapper.selectHL(purma.getMA021());
            purtg.setTg008(hl);
        }
        purtg.setTg009(purma.getMA030());
        purtg.setTg010(jhdDto.getTC018());
        purtg.setTg012(0.0);
        purtg.setTg013("N");

        purtg.setTg014(yyyyMMdd);
        purtg.setTg015("N");
        purtg.setTg016(jhdDto.getTG016());
        List<Purth> purths = new ArrayList<>();
        List<PurchaseDto> purchaseDtos = jhdDto.getPurchaseDtos();
        for (PurchaseDto purchaseDto : purchaseDtos) {
            Purth purth = new Purth();
            String xuh = String.format("%04d", i++);
            Integer pd = purthMapper.isPurchaseOrder(purchaseDto.getCgdb(), purchaseDto.getCgdh(), purchaseDto.getXh(), purtg.getTg005());
            if (pd < 0) {
                return RpcServiceResult.getFailureResult("-1000", "not fount Inspection batch Please re-select");
            } else {
                list = purthMapper.SelectByPur(jhdDto.getTC018(),purchaseDto.getCgdb(), purchaseDto.getCgdh(), purchaseDto.getXh(), purtg.getTg005());
            }
            if(list==null){
                return RpcServiceResult.getFailureResult("-1001", "采购单获取不到");
            }
            purth.setCompany("ttc");
            purth.setCreator(jhdDto.getCreator());
            purth.setUsrGroup(purtgMapper.getUsrGroup(jhdDto.getCreator()));
            purth.setCreateDate(currentTime.format(create_date));
            purth.setFlag("2");
            purth.setTh001("3420");
            purth.setTh002("" + dh);
            purth.setTh003(xuh);
            purth.setTh004((String) list.get("TD004"));
            purth.setTh005((String) list.get("TD005"));
            purth.setTh006((String) list.get("TD006"));
            BigDecimal bigDecimal1 = (BigDecimal) list.get("TD008");
            BigDecimal bigDecimal2 = (BigDecimal) list.get("TD015");
            Double sum = bigDecimal1.doubleValue() - bigDecimal2.doubleValue();
            if (purchaseDto.getJhsl()==null || purchaseDto.getJhsl() == 0.0) {
                purth.setTh007(sum);
                jhsl += sum;
            }else {
                purth.setTh007(purchaseDto.getJhsl());
                jhsl += purchaseDto.getJhsl();
            }
            purth.setTh008((String) list.get("TD009"));
            if (purchaseDto.getStashNo()!=null && !purchaseDto.getStashNo().isEmpty()){
                purth.setTh009(purchaseDto.getStashNo());
            }else {
                purth.setTh009((String) list.get("TD007"));
            }
            if (purchaseDto.getLot()!=null && !purchaseDto.getLot().isEmpty()){
                purth.setTh010(purchaseDto.getLot());
            }else {
                purth.setTh010("********************");
            }
            purth.setTh011((String) list.get("TD001"));
            purth.setTh012((String) list.get("TD002"));
            purth.setTh013((String) list.get("TD003"));
            purth.setTh014(yyyyMMdd);
            if (purchaseDto.getYssl()==null || purchaseDto.getYssl()==0.0) {
                purth.setTh015(sum);
                zyssl += sum;
            }else {
                purth.setTh015(purchaseDto.getYssl());
                zyssl += purchaseDto.getYssl();
            }

            RaxDto raxDto = new RaxDto(purma.getMA044(),
                    BigDecimal.valueOf(purchaseDto.getJjsl()),
                    BigDecimal.valueOf(purma.getMA064().doubleValue()),
                    BigDecimal.valueOf(purchaseDto.getSinglePrice()),
                    BigDecimal.valueOf(hl));

            RaxVo raxVo = MoneyUtils.getRax(raxDto);

            Double jjsl = (purchaseDto.getJjsl());
            purth.setTh016(jjsl);
            purth.setTh017(0.000000);
            Double DJ = 0.0;
            if (purchaseDto.getSinglePrice()!=null){
                DJ = purchaseDto.getSinglePrice();
            }else {
                DJ = purtcdMapper.selectOne((String) list.get("TD002"), (String) list.get("TD003"));
            }
            purth.setTh018(DJ);
            purth.setTh019(raxVo.getPrice().doubleValue());
            jhje += purth.getTh019();
            purth.setTh020(0.000000);
            kkje = 0.000000;
            purth.setTh024(0.00);
            purth.setTh026("N");
            Boolean CQ = purtcdMapper.selectCQ((String) list.get("TD002"), (String) list.get("TD003"));
            if (CQ) {
                purth.setTh027("Y");
            } else {
                purth.setTh027("N");
            }
            purth.setTh028("2");
            purth.setTh029("N");
            purth.setTh030("N");
            purth.setTh031("N");
            purth.setTh032("N");
            purth.setTh033(purchaseDto.getBz());
            if (purchaseDto.getYssl()==null || purchaseDto.getYssl()==0.0) {
                purth.setTh034(sum);
            }else {
                purth.setTh034(purchaseDto.getYssl());
            }
            purth.setTh038(jhdDto.getCreator());
            purth.setTh043("N");
            purth.setTh044("N");
            //TODO 这边取得不够完善
            purth.setTh045(raxVo.getPriceBeforeTax().doubleValue());
            purth.setTh046(raxVo.getTaxPrice().doubleValue());
            ybse += raxVo.getTaxPrice().doubleValue();
            purth.setTh047(raxVo.getPriceBeforeTaxLocal().doubleValue());
            purth.setTh048(raxVo.getTaxPriceLocal().doubleValue());
            bbse += raxVo.getTaxPriceLocal().doubleValue();
            BigDecimal td030 = (BigDecimal) list.get("TD030");
            BigDecimal td031 = (BigDecimal) list.get("TD031");
            purth.setTh049(td030.doubleValue()-td031.doubleValue());
            jhbzsl += td030.doubleValue()-td031.doubleValue();
            purth.setTh050(td030.doubleValue()-td031.doubleValue());
            zysbzsl += td030.doubleValue()-td031.doubleValue();
            purth.setTh051(0.00);
            purth.setTh052(0.00);
            purth.setTh053((String) list.get("TD032"));
            purth.setTh054(0.000000);
            purth.setTh055(0.00);
            BigDecimal CD031 = (BigDecimal) list.get("TD036");
            purth.setTh058(CD031.doubleValue());
            BigDecimal CD032 = (BigDecimal) list.get("TD037");
            purth.setTh059(CD032.doubleValue());
            zjs += CD032.doubleValue();
            purth.setTh060(0.0000);
            purth.setTh061(0.00);
            purth.setTh062(0.00);
            purth.setTh064((String) list.get("TD038"));
            purth.setTh065((String) list.get("TD009"));
            purth.setTh068(0.000000);
            purth.setTh069(0.000000);
            purth.setTh070("N");
            purth.setTh071(1.0000);
            purth.setTh072("##########");
            purth.setTh077(0.000000);
            purth.setTh078(0.000000);
            BigDecimal TD045 = (BigDecimal) list.get("TD045");
            BigDecimal TD046 = (BigDecimal) list.get("TD046");
            purth.setTh079(TD045.doubleValue()- TD046.doubleValue());
            purth.setTh080("N");
            BigDecimal TDC01 = (BigDecimal) list.get("TDC01");
            BigDecimal TDC02 = (BigDecimal) list.get("TDC02");
            purth.setThc01(TDC01.doubleValue()-TDC02.doubleValue());
            purth.setThc02("2");
            purths.add(purth);
        }
        //TODO 进货金额需要去采购单里面取
        purtg.setTg017(jhje);
        purtg.setTg018(kkje);
        purtg.setTg019(ybse);
        purtg.setTg020(jhje-kkje);
        purtg.setTg021(purma.getMA003());
        purtg.setTg022(purma.getMA005());
        purtg.setTg023("1");
        purtg.setTg023("N");
        purtg.setTg025(zjs);
        purtg.setTg026(zyssl);
        purtg.setTg028(jhje);
        purtg.setTg030(purma.getMA064().doubleValue());
        purtg.setTg031(bbdkje);
        purtg.setTg032(bbse);
        purtg.setTg038(0.000000);
        purtg.setTg039(0.000000);
        purtg.setTg040(zysbzsl);
        purtg.setTg041(0.000000);
        purtg.setTg042("N");
        purtg.setTg045(0.0);
        purtg.setTg046(0.0);
        purtg.setTg050("N");
        purtg.setTg053(jhsl);
        purtg.setTg054(jhbzsl);
        purtg.setTg055(0.000000);
        purtg.setTg058("0");
        purtg.setUdf01(jhdDto.getUDF01());
        purtgMapper.insert(purtg);

        for (Purth purth1 : purths) {
            purthMapper.insert(purth1);
        }

        ShenHe shenHe = new ShenHe();
        shenHe.setCom("ttc");
        shenHe.setProgram("PURI09");
        shenHe.setUser(jhdDto.getCreator());
        shenHe.setDb("3420");
        shenHe.setDh(""+dh);
        shenHe.setType("DO");
        shenHe.setRq(yyyyMMdd);

        InetAddress localhost = InetAddress.getLocalHost();
        String ipAddress = localhost.getHostAddress();
        String url = "http://" + ipAddress + ":9000/api/yifei/audit";


        String jsonString = JSON.toJSONString(shenHe);
        String ans = null;
        try {
//            ShenHe anss=restTemplate.postForObject(url,shenHe,ShenHe.class);
            ans = httpClientService.postRequest(url, jsonString);
        }catch (Exception e){
            //System.out.println("出现异常请查看审批后台是否开启");
            log.info("出现异常请查看审批后台是否开启");
            System.out.println(e);
            return RpcServiceResult.getFailureResult("-10000", "出现异常请查看审批后台是否开启");
        }

        HttpResult httpResult = new HttpResult();

        if (ans == null)
        {
            System.out.println("出现异常请查看审批后台是否开启");
            return RpcServiceResult.getFailureResult("-10000", "出现异常请查看审批后台是否开启");
        }
        else {
            ans = ans.replace("\\r","");

            System.out.println("ans :"  + ans);
            httpResult = JSON.parseObject(ans,HttpResult.class);
//            System.out.println("httpResult: " + httpResult);

        }
        if (httpResult.getStatus().equals("失败"))
        {
            purtgMapper.deletePurtg(purtg);
            for (Purth purth1 : purths) {
                purthMapper.deletePurth(purth1);
            }
            return RpcServiceResult.getFailureResult("-10001", "审核失败！\\n" + "原因：" + httpResult.getReason() + "\\n 详情：" + httpResult.getDetail());
        }


        Map<String, Object> map = new HashMap<>();
        map.put("db-dh","3420-"+dh);
        map.put("result", "success");
        return RpcServiceResult.getSuccessResult(map);
    }

    @Override
    public Page<PURTCD> selectPurtcdcdPage(Integer page, Integer pageSize, String startcreate, String endcreate, String startupdate, String endupdate) {
        List<PURTC> purtcListAll =
                purtcdMapper.selectPurtcListByCreateDateWithModiDate(startcreate,endcreate,startupdate,endupdate);
        List<PURTCD> purtcdList = new ArrayList<>();
        int total = purtcListAll.size();
        int start = (page - 1 ) * pageSize;
        int end  = page * pageSize;
        //if(end > total) end = total;
        if (end <= total){
            for (int i = start; i < end; i++) {
                PURTCD purtcd = new PURTCD();
                purtcd.setPurtc(purtcListAll.get(i));
                List<PURTD> purtdList = purtcdMapper.selectPurtdList(purtcListAll.get(i).getTC001(),purtcListAll.get(i).getTC002());
                purtcd.setPurtdList(purtdList);
                purtcdList.add(purtcd);
            }
        }
        else if(end > total && start < end ){
            for (int i = start; i < total; i++) {
                PURTCD purtcd = new PURTCD();
                purtcd.setPurtc(purtcListAll.get(i));
                List<PURTD> purtdList = purtcdMapper.selectPurtdList(purtcListAll.get(i).getTC001(),purtcListAll.get(i).getTC002());
                purtcd.setPurtdList(purtdList);
                purtcdList.add(purtcd);
            }
        }



        Page<PURTCD> purtcdPage = new Page<>(page,pageSize);
        purtcdPage.setRecords(purtcdList);
        purtcdPage.setTotal(purtcListAll.size());
        return purtcdPage;
    }

    @Override
    public RpcServiceResult backJhd(backDto backDto) throws UnknownHostException {
        LocalDateTime currentTime = LocalDateTime.now();
        DateTimeFormatter yyyyMMdd = DateTimeFormatter.ofPattern("yyyyMMdd");
        ShenHe shenHe = new ShenHe();
        shenHe.setCom("ttc");
        shenHe.setProgram("PURI09");
        shenHe.setUser(backDto.getCreator());
        shenHe.setDb(backDto.getDb());
        shenHe.setDh(backDto.getDh());
        shenHe.setType("UNDO");
        shenHe.setRq(currentTime.format(yyyyMMdd));

        InetAddress localhost = InetAddress.getLocalHost();
        String ipAddress = localhost.getHostAddress();
        String url = "http://" + ipAddress + ":9000/api/yifei/audit";

        String jsonString = JSON.toJSONString(shenHe);
        String ans = null;
        try {
            ans = HttpClientUtil.postRequest(url, jsonString);
            HttpResult httpResult = JSON.parseObject(ans, HttpResult.class);
            if (httpResult.getStatus().equals("失败")) {
                return RpcServiceResult.getFailureResult("-10001","撤销入库单："+backDto.getDb()+"-"+backDto.getDh()+"失败");
            }
        } catch (Exception e) {
            System.out.println("出现异常请查看审批后台是否开启");
            return RpcServiceResult.getFailureResult("-10001","撤销入库单："+backDto.getDb()+"-"+backDto.getDh()+"失败");
        }
        purtgMapper.delete(backDto.getDb(),backDto.getDh());
        purthMapper.delete(backDto.getDb(),backDto.getDh());
        return RpcServiceResult.getSuccessResult("撤销入库单："+backDto.getDb()+"-"+backDto.getDh()+"成功");
    }

}
